<template>
  <el-config-provider :size="'default'" :locale="zhCn" >
    <router-view/>
    <loader v-show="loading" />
    <JDialog ref="JDialogRef" />
  </el-config-provider>
</template>

<script setup name="app">
import { onMounted, watch, ref } from 'vue';
import { useRoute } from 'vue-router';
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import bus from '@/store/bus';
import Loader from '@/lui/loader.vue';
import JDialog from '@/lui/JEditor.vue';
import tool from "@/utils/tool";
// 定义变量内容
const route = useRoute();
const loading = ref(false);
const JDialogRef = ref();
// 页面加载时
onMounted(() => {
  bus.$on('doLoading', doLoading);
  bus.$on('openEdit', openEdit);
  scanCode();
})
const doLoading = (val) => {
  loading.value = val;
  setTimeout(() => {
    loading.value = false
  }, 5000)
}
const openEdit = ({ title, params }) => {
  JDialogRef.value.open({ title, params })
}
const scanCode = () => {
  let str = '';
  document.onkeydown = function (e) {
    if (e.target.tagName !== 'BODY') {
      str = '';
    } else {
      if (e.key !== 'Enter') {
        if (['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-'].indexOf(e.key) !== -1) {
          let k = e.key;
          str += k;
        }
      } else {
        if (str) {
          if (!str) return;
          // let bill = str.split('-');
          // if (bill.length !== 2) return;
          scanRet(str)
        }
        str = '';
      }
    }
  };
}
const scanRet = (str) => {
  if(!str) return  tool.err('未读取到数据')
  // let arr = str.split('-')
  // if(arr.length !== 2) return tool.err('读取数据失败')
  // let transType = Number(arr[0])
  // let trans = [150501,150502,150601,150602,150603,153001,153101,153201,150706,150806]
  // if (trans.indexOf(transType) === -1) {
  //   return tool.err('还未支持此类型票据扫描')
  // }
  bus.$emit('scan-ret', {code: str});
  // tool.post('bill/getDetailByNo',{code: str}).then(ret => {
  //   if(ret.stat) {
  //     bus.$emit('BillScan', ret);
  //   }
  // })
}
// 监听路由的变化，设置网站标题
watch(
    () => route.path,
    () => {
    },
    {
      deep: true,
    }
)
</script>